package postpc.powerreminders.com.ui.activities;

import postpc.powerreminders.com.db.RotgDatabase;

import postpc.powerreminders.com.R;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;

public class LocationsActivity extends ListActivity {
	
	private static final String TAG = "LocationsActivity";

	private Cursor m_AllLocations;

	public LocationsActivity() {
		
		
		Log.d(TAG, "CTOR");
	}
	
	private void reloadLocations() {
		RotgDatabase db = RotgDatabase.getInstance();
		m_AllLocations = db.fetchAllLocations();


		
		ListView listView = getListView();
		
		registerForContextMenu(listView);
		
		CursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.location_item, m_AllLocations, 
				new String[] { RotgDatabase.NAME } , 
				new int[] { postpc.powerreminders.com.R.id.location_item_name });
				
		listView.setAdapter(adapter);

		db.close();
	}
		
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "Init location manager");
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");		
		
		reloadLocations();
		
		ListView listView = getListView();
		
		// Get rid of the annoying text filter
		listView.setTextFilterEnabled(false);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			
			public void onItemClick(AdapterView<?> parent, View view,
			        int position, long id) {
				Intent editLocationIntent = new Intent();
				editLocationIntent.setClassName(getApplicationContext(), LocationEditActivity.class.getName());
				editLocationIntent.putExtra("id", id);
				Log.d(TAG, "Opening edit for location id="+id);
				startActivityForResult(editLocationIntent, 0);
			}
		});
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.locations_menu, menu);
		return true;
	}


	private void newLocation() {
		Intent newLocationIntent = new Intent();
		newLocationIntent.setClassName(getApplicationContext(), LocationEditActivity.class.getName());
		newLocationIntent.setAction(Intent.ACTION_EDIT);
		startActivityForResult(newLocationIntent, 0);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		Log.d(TAG, "OnActivityResult");
		reloadLocations();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.new_location_menu_item:
			newLocation();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		
		if (v.getId() == getListView().getId()) {
			menu.add(0, 0, 0, "Delete");
		}
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		int menuItemIndex = item.getItemId();
		
		RotgDatabase db = RotgDatabase.getInstance();
		
		Log.d("IMPORTANT", "menu item index = " + menuItemIndex + " For reminder_id = " + info.id);
		if (menuItemIndex == 0) {
			db.deleteLocation(info.id);
			reloadLocations();
		}
		return true;
	}
}
